
// A user can add a URL to their personal bookmarks list 
// and associate it with a title and a list of tags.
// Users share URLs and tags in the system. The system keeps
// track of how many users associate with a given URL and
// how many URLs are associated with a given tag.
// So what the system essentially offers to users,
// is the ability to search for URLs users saved in the system,
// either according to the actual URL address, titles, or tags.
// They can also browse the see the list of tags in the system and
// browse all URLs associated with a given tag.
// Finally, users can also see the list of users in the system and
// browse their personal collection of bookmarks.

application socbm

imports user_data
imports user_ui

imports bookmark_data
imports bookmark_ui

imports tag_data
imports tag_ui

imports access_rules

//
// Application global state
//

entity SocBMWiki {
  text		::	WikiText
  editors	->	Set<User>
}

var admin := User{name:="admin", pass:=("admin" as Secret).digest(), realname:="administrator"}
// One SocBM Wiki per page:
var editwikiwiki		:= SocBMWiki{text:="###Edit this wiki.", 		editors:={admin}}
var rootwiki			:= SocBMWiki{text:="###Homepage of SocBM.",		editors:={admin}}
var signinwiki			:= SocBMWiki{text:="###Sign In.",			editors:={admin}}
var registerwiki		:= SocBMWiki{text:="###Create new account.",		editors:={admin}}
var signoffwiki			:= SocBMWiki{text:="###Sign off.",			editors:={admin}}
var homepagewiki		:= SocBMWiki{text:="###Home.",				editors:={admin}}
var bookmarkswiki		:= SocBMWiki{text:="###System Bookmarks.",		editors:={admin}}
var bookmark_referenceswiki	:= SocBMWiki{text:="###Bookmark References.",		editors:={admin}}
var tagswiki			:= SocBMWiki{text:="###System Tags.",			editors:={admin}}
var tag_referenceswiki		:= SocBMWiki{text:="###Tag References.",		editors:={admin}}
var tagentry_referenceswiki	:= SocBMWiki{text:="###Tag Entry References.",		editors:={admin}}
var userswiki			:= SocBMWiki{text:="###System Users.",			editors:={admin}}
var bookmarkentry_createwiki	:= SocBMWiki{text:="###Create new Bookmark Entry.",	editors:={admin}}
var bookmarkentry_editwiki	:= SocBMWiki{text:="###Edit this Bookmark Entry.",	editors:={admin}}

// In the root page, users see their homepage, guest see the login/registration form.
define page root() 
{main(rootwiki){

  init {
    if(securityContext.loggedIn) {
      return homepage(securityContext.principal, false, "");
    }
  }

}}


define no-span template topmenu()
{
  list[class:=hmenu, id:=topmenu] {
    listitem[id:=logo] { navigate(root()) 	{"SocBM"} }
    listitem { navigate(bookmarks(false, ""))	{"Bookmarks"}}
    listitem { navigate(tags())			{"Tags"}}
    listitem { navigate(users())		{"Users"}}
    listitem[id:=signoff] { navigate(signoff())	{output(securityContext.principal.realname)"|sign off"}}
    listitem[id:=signin]  { navigate(signin()) 	{"sign in"}}
    listitem { topmenu_shortcuts()}
  }
}

define no-span template topmenu_shortcuts()
{
  list[class:=hmenu,id:=topmenu_shortcuts] {
    listitem {bookmarkentry_create_box()}
  }
}

define template bookmarkentry_create_box() 
{
  form  {
    var url : String
    input(url) 
    actionLink("save new bookmark", action{return bookmarkentry_create(url);})	
  }
}

define template main(wiki: SocBMWiki)
{
  topmenu()
  messages()
  block[id:=mainwiki] {
    output(wiki.text)
    navigate(editwiki(wiki)){"[EDIT]"}  
  }
  elements()
}

define page editwiki(wiki: SocBMWiki)
{main(editwikiwiki){
  form {
    table {
      var text := wiki.text
      row {
        input(text)
      }
      row {
        action("Save changes", action{wiki.text := text; return root();})
      }
    }
  }
}}